package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.elinkway.infinitemovies.bean.Episode;
import com.elinkway.infinitemovies.ui.activity.VideoDetailActivity;
import com.elinkway.infinitemovies.utils.NetWorkTypeUtils;
import com.elinkway.infinitemovies.utils.ToastUtil;
import com.le123.ysdq.R;
import com.le123.ysdq.databinding.LayoutEpisodePlayItemBinding;

import java.util.ArrayList;

/**
 * Created by xiao on 21/4/17.
 */
public class EpisodePlayAdapter extends RecyclerView.Adapter<EpisodePlayAdapter.EpisodeHolder>  {

    private Context mContext;
    private ArrayList<Episode> episodes;
    private String mType;
    private String mPage;
    private LayoutInflater mLayoutInflater;
    private int index;

    private String mSelectProder = "";

    private OnItemClickListener mListener;

    public EpisodePlayAdapter(Context mContext, ArrayList<Episode> episodes, String type, String page, int index) {
        this.mContext = mContext;
        this.episodes = episodes;
        this.mType = type;
        this.index = index;
        mLayoutInflater = LayoutInflater.from(mContext);
        this.mPage = page;
        if (!TextUtils.isEmpty(((VideoDetailActivity)mContext).currentPlayingPorder())) {
            mSelectProder = ((VideoDetailActivity) mContext).currentPlayingPorder();
        }
    }

    @Override
    public EpisodeHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        return new EpisodeHolder(mLayoutInflater.inflate(R.layout.layout_episode_play_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(EpisodeHolder holder, int position) {
        if(holder instanceof EpisodeHolder){
            holder.bind(episodes.get(position), position, mContext, mType, mPage, mSelectProder, index);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    public class EpisodeHolder extends RecyclerView.ViewHolder {
        private LayoutEpisodePlayItemBinding mEpisodeBinding;

        public EpisodeHolder(View itemView) {
            super(itemView);
            mEpisodeBinding = DataBindingUtil.bind(itemView);
        }

        public void bind(@NonNull final Episode episode, final int position, final Context context, final String type, String page, String mSelectProder, final int index) {
            if("halfScreen".equalsIgnoreCase(page)){
                mEpisodeBinding.episodePlayItem.setBackgroundResource(R.drawable.rectangle_round_corner_f5);
                mEpisodeBinding.episodePlayItemNum.setTextColor(context.getResources().getColor(R.color.blue_black_text_selecter));
                mEpisodeBinding.ispay.setImageResource(R.drawable.is_pay);
            }else if("fullScreen".equalsIgnoreCase(page)){
                mEpisodeBinding.episodePlayItem.setBackgroundResource(R.drawable.rectangle_round_corner_black60);
                mEpisodeBinding.episodePlayItemNum.setTextColor(context.getResources().getColor(R.color.blue_and_white_selector));
                mEpisodeBinding.ispay.setImageResource(R.drawable.ispay_short);
            }


            if("list".equals(type)){
                mEpisodeBinding.episodePlayItemNum.setText(episode.getSubName());
                mEpisodeBinding.episodePlayItemNum.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
            }else if("grid".equals(type)){

                mEpisodeBinding.episodePlayItemNum.setText(episode.getPorder());
                mEpisodeBinding.episodePlayItemNum.setGravity(Gravity.CENTER);
            }
            if (mSelectProder.equals(episode.getPorder())) {
                mEpisodeBinding.episodePlayItemNum.setSelected(true);
                mEpisodeBinding.episodePlayItemNum.setTextColor(context.getResources().getColor(R.color.color_3599f8));
                TextPaint paint = mEpisodeBinding.episodePlayItemNum.getPaint();
                paint.setFakeBoldText(true);
            } else {
                TextPaint paint = mEpisodeBinding.episodePlayItemNum.getPaint();
                paint.setFakeBoldText(false);
                mEpisodeBinding.episodePlayItemNum.setSelected(false);
            }
            if ("1".equals(episode.getIsPay())){
                mEpisodeBinding.ispay.setVisibility(View.VISIBLE);
                mEpisodeBinding.episodePlayItemNum.setTextColor(context.getResources().getColor(R.color.color_bebebe));
            }else {
                mEpisodeBinding.ispay.setVisibility(View.GONE);
            }
            mEpisodeBinding.episodePlayItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(context instanceof VideoDetailActivity){
                        String isPay=episode.getIsPay();
                        //付费点击无效
                        if ("1".equals(isPay)) return;

                        int netType = NetWorkTypeUtils.reportNetType(mContext);
                        if (netType == NetWorkTypeUtils.NETTYPE_NO || netType == NetWorkTypeUtils.TYPE_ERROR) {
                            ToastUtil.showShortToast(mContext, mContext.getString(R.string.neterror));
                            return;
                        }

                        ((VideoDetailActivity)context).showNetDialog(episode.getPosition(), "grid".equals(type));
                        ((VideoDetailActivity)context).updateEpisodeStatus(episode.getPosition());
                        ((VideoDetailActivity)context).setmCurrentPlayingPorder(episode.getPorder());
                        setmSelectProder(episode.getPorder());
                        notifyDataSetChanged();
                    }
                }
            });
        }
    }

    public String getmSelectProder() {
        return mSelectProder;
    }

    public void setmSelectProder(String mSelectProder) {
        if (TextUtils.isEmpty(mSelectProder)) {
            this.mSelectProder = "";
        } else {
            this.mSelectProder = mSelectProder;
        }
        notifyDataSetChanged();
    }

    public void setEpisodes(ArrayList<Episode> episodes) {
        this.episodes=episodes;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return episodes.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, final int i);
    }
}

